﻿using SelfMadeMillionaire.Core;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Web.Infrastructure.Controllers;
using SelfMadeMillionaire.Web.Infrastructure.Formatters;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PagedList;
using System.Globalization;

namespace SelfMadeMillionaire.Web.Infrastructure.Controllers.Admin
{
	public class ReportsController : AdminController
	{
		IReportsService _reportsService;
        IPaymentService _paymentService;

        public ReportsController(IReportsService reportsService, IPaymentService paymentService)
		{
			_reportsService = reportsService;
            _paymentService = paymentService;
		}

        public ActionResult NewUser(string reportType)
        {
            if (string.IsNullOrEmpty(reportType))
                reportType = "today";

            NewUserReportViewModel model = new NewUserReportViewModel();
            model.ReportType = reportType;
            model.Items = new List<NewUserReportItemModel>();

            NewUserReportItemModel item = new NewUserReportItemModel();
            item.DateRange = model.DateRange;
            item.CountFree = _reportsService.NewUser(model.BegDate, model.EndDate, SubscriptionType.Free);
            item.CountSilver = _reportsService.NewUser(model.BegDate, model.EndDate, SubscriptionType.Silver);
            item.CountGold = _reportsService.NewUser(model.BegDate, model.EndDate, SubscriptionType.Gold);
            item.CountPlatinum = _reportsService.NewUser(model.BegDate, model.EndDate, SubscriptionType.Platinum);
            item.CountTotal = _reportsService.NewUser(model.BegDate, model.EndDate, null);
            model.Items.Add(item);

            item = new NewUserReportItemModel();
            DateTime startDateEmpty = new DateTime(2000, 1, 1);
            DateTime endDateEmpty = new DateTime(2999, 1, 1);
            item.DateRange = "All the period";
            item.CountFree = _reportsService.NewUser(startDateEmpty, endDateEmpty, SubscriptionType.Free);
            item.CountSilver = _reportsService.NewUser(startDateEmpty, endDateEmpty, SubscriptionType.Silver);
            item.CountGold = _reportsService.NewUser(startDateEmpty, endDateEmpty, SubscriptionType.Gold);
            item.CountPlatinum = _reportsService.NewUser(startDateEmpty, endDateEmpty, SubscriptionType.Platinum);
            item.CountTotal = _reportsService.NewUser(startDateEmpty, endDateEmpty, null);
            model.Items.Add(item);
                        
            if (Request.IsAjaxRequest())
                return PartialView("NewUserList", model);

            return View(model);
        }

        public ActionResult DailyNewUser()
        {
            DailyNewUserReportViewModel model = new DailyNewUserReportViewModel();
            model.Items = new List<DailyNewUserReportItemModel>();
            
            DateTime reportDate = model.BegDate;
            while (reportDate <= model.EndDate)
            {
                DailyNewUserReportItemModel item = new DailyNewUserReportItemModel();
                item.Date = reportDate;
                item.CountFree = _reportsService.NewUser(reportDate, reportDate, SubscriptionType.Free);
                item.CountSilver = _reportsService.NewUser(reportDate, reportDate, SubscriptionType.Silver);
                item.CountGold = _reportsService.NewUser(reportDate, reportDate, SubscriptionType.Gold);
                item.CountPlatinum = _reportsService.NewUser(reportDate, reportDate, SubscriptionType.Platinum);
                item.CountTotal = _reportsService.NewUser(reportDate, reportDate, null);

                model.Items.Add(item);

                reportDate = reportDate.AddDays(1);
            }
            
            if (Request.IsAjaxRequest())
                return PartialView("DailyNewUserList", model);

            return View(model);
        }

        public ActionResult SubscriptionPayments(int? page, string sort)
        {
            int pageIndexTrans = page ?? 1;
            int pageSize = 10;

            var model = new SubscriptionPaymentsReportViewModel();
            model.SortOrder = sort;

            var list = _paymentService.GetPayments();
            if (!string.IsNullOrEmpty(sort))
            {
                string[] sortArr = sort.Split(',');
                if (sortArr[1] == "0")
                {

                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderBy(i => i.Payment.Order.Subscriber.FirstName);
                            break;
                        case "1":
                            list = list.OrderBy(i => i.Payment.Order.Subscriber.LastName);
                            break;
                        case "2":
                            list = list.OrderBy(i => i.Payment.Order.Subscriber.Email);
                            break;
                        case "3":
                            list = list.OrderBy(i => i.Payment.ItemName);
                            break;
                        case "4":
                            list = list.OrderBy(i => i.Payment.PaymentDate);
                            break;
                        case "5":
                            list = list.OrderBy(i => i.Payment.McGross);
                            break;
                    }

                }
                else
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderByDescending(i => i.Payment.Order.Subscriber.FirstName);
                            break;
                        case "1":
                            list = list.OrderByDescending(i => i.Payment.Order.Subscriber.LastName);
                            break;
                        case "2":
                            list = list.OrderByDescending(i => i.Payment.Order.Subscriber.Email);
                            break;
                        case "3":
                            list = list.OrderByDescending(i => i.Payment.ItemName);
                            break;
                        case "4":
                            list = list.OrderByDescending(i => i.Payment.PaymentDate);
                            break;
                        case "5":
                            list = list.OrderByDescending(i => i.Payment.McGross);
                            break;
                    }
                }
            }
            else
                list = list.OrderByDescending(i => i.Payment.ID);

            model.Transactions = list.ToPagedList(pageIndexTrans, pageSize);
            
            if (Request.IsAjaxRequest())
                return PartialView("SubscriptionPaymentsList", model);

            return View(model);
        }        
	}
}
